package com.metal.gear.certification.entity.dto;

import com.metal.gear.certification.entity.po.SysUser;

import com.metal.gear.common.annotation.Dict;
import com.metal.gear.common.constant.CommonConstant;
import com.metal.gear.common.util.PasswordUtil;
import com.metal.gear.common.util.oConvertUtils;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.LocalDate;
import java.util.List;

@Data
public class UserSaveDto {

    @Schema(name ="用户账号")
    private String username;

    @Schema(name ="登录密码")
    private String password;

    @Schema(name ="确认密码")
    private String passwordConfirm;

    @Schema(name ="用户姓名")
    private String realname;

    @Schema(name ="工号")
    private String workNo;

    @Schema(name ="手机号码")
    private String phone;

    @Schema(name ="职务")
    private String positionCode;

    @Schema(name ="角色")
    private List<String> roleIdList;

    @Schema(name ="部门")
    private List<String> departIdList;

    @Schema(name ="生日")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private LocalDate birthday;

    @Dict(dicCode = "sex")
    @Schema(name ="性别")
    private Integer sex;

    @Schema(name ="电子邮件")
    private String email;

    @Schema(name ="座机号")
    private String telephone;

    public static SysUser translateTo(UserSaveDto saveDto){
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(saveDto,sysUser);
        sysUser.setPost(saveDto.getPositionCode());

        String salt = oConvertUtils.randomGen(8);
        sysUser.setSalt(salt);
        String passwordEncode = PasswordUtil.encrypt(sysUser.getUsername(), sysUser.getPassword(), salt);
        sysUser.setPassword(passwordEncode);
        sysUser.setStatus(1);
        sysUser.setDelFlag(CommonConstant.DEL_FLAG_0);
        return sysUser;
    }

}
